package sg.edu.nus.iss.billing.parse;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import sg.edu.nus.iss.billing.entity.transaction.CallTransaction;
import sg.edu.nus.iss.billing.entity.transaction.CallType;
/**
 * @author sammil
 *
 */
public class TransactionFileParser {

	HashMap<String, ArrayList<CallTransaction>> outputMap;

	public HashMap<String, ArrayList<CallTransaction>> parse(
			String inputFileName) throws IOException {
		LineNumberReader in = null;
		try {
			outputMap = new HashMap<String, ArrayList<CallTransaction>>();
			in = new LineNumberReader(new FileReader(inputFileName));
			ParserState current = TransactionAccountHeaderState.SINGLETON;
			while (current != FinishedState.SINGLETON) {
				System.out.println("current:" + current);
				current = current.parse(in, outputMap);
			}
			return outputMap;
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<CallTransaction> getCallListByAccount(String accountNo) {
		List<CallTransaction> callTransFilteredList =null;
		if (outputMap.containsKey(accountNo)) {
			callTransFilteredList =  outputMap.get(accountNo);
			Collections.sort(callTransFilteredList);
		}
		return callTransFilteredList;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<CallTransaction> getCallListByAccount(String accountNo,CallType callType) {
		ArrayList<CallTransaction> callTransFilteredList = new ArrayList<CallTransaction>();
		if (outputMap.containsKey(accountNo)) {
			ArrayList<CallTransaction> callTransList = outputMap.get(accountNo);
			if (callTransList != null)
				for (CallTransaction callTxn : callTransList) {
					if (callTxn.getCallType() == callType) {
						callTransFilteredList.add(callTxn);
					}
				}
		}
		Collections.sort(callTransFilteredList);
		return callTransFilteredList;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<CallTransaction> getCallListByAccount(String accountNo,String hostNo) {
		ArrayList<CallTransaction> callTransFilteredList = new ArrayList<CallTransaction>();
		if (outputMap.containsKey(accountNo)) {
			ArrayList<CallTransaction> callTransList = outputMap.get(accountNo);
			if (callTransList != null)
				for (CallTransaction callTxn : callTransList) {
					if (callTxn.getHostNumber().equals(hostNo)) {
						callTransFilteredList.add(callTxn);
					}
				}
		}
		Collections.sort(callTransFilteredList);
		return callTransFilteredList;
	}
	@SuppressWarnings("unchecked")
	public ArrayList<CallTransaction> getCallListByAccount(String accountNo,String hostNo, CallType callType) {
		ArrayList<CallTransaction> callTransFilteredList = new ArrayList<CallTransaction>();
		if (outputMap.containsKey(accountNo)) {
			ArrayList<CallTransaction> callTransList = outputMap.get(accountNo);
			if (callTransList != null)
				for (CallTransaction callTxn : callTransList) {
					if (callTxn.getHostNumber().equals(hostNo)
							&& callTxn.getCallType() == callType) {
						callTransFilteredList.add(callTxn);
					}
				}
		}
		Collections.sort(callTransFilteredList);
		return callTransFilteredList;
	}

	public static void main(String s[]) {

		System.out.println("Start testing");
		String inputFileName = "";
		try {
			inputFileName = (new File(".").getCanonicalPath().concat("/data/transaction-data.txt"));
			System.out.println("inputFileName :" + inputFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TransactionFileParser transactionFileParser = new TransactionFileParser();
		try {
			transactionFileParser.parse(inputFileName);
			ArrayList<CallTransaction> callTransList = transactionFileParser
					.getCallListByAccount("1.14647314M", "68786611", CallType.IDD);
			System.out.println("Size " + callTransList.size());
			for (CallTransaction callList1 : callTransList) {
				System.out.println("callList1 :" + callList1);
			}


		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Done!");
	}
}
